﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;

namespace Circa {

    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App: Application {
        public List<Protocol.Irc.Network> Networks { get; set; }

        private Configuration.Root configuration;

        public App( ): base( ) {
            Networks = new List<Protocol.Irc.Network>( );
        }

        private Configuration.Root InventConfiguration( ) {
            return new Configuration.Root( ) {
                Networks = new List<Configuration.Network> {
                    new Configuration.Network {
                        Identities = new List<Configuration.Identity> {
                            new Configuration.Identity {
                                Name = "Ice Karma - default",
                                Nicks = new List<string> {
                                    "IceKarma"
                                },
                                UserName = "foo",
                                RealName = "Don't cry, you'll still be loved..."
                            },
                        },
                        Servers = new List<Configuration.Server> {
                            new Configuration.Server {
                                Name = "sekhmet.zive.ca",
                                HostNames = new List<string> {
                                    "sekhmet.zive.ca"
                                },
                                Ports = new List<ushort> {
                                    16001,
                                }
                            },
                        },
                        Channels = new List<Configuration.Channel> {
                            new Configuration.Channel {
                                Name = "#circatesting",
                                Description = "Channel for Circa testing"
                            },
                        },
                        AutoConnect = true,
                        Name = "icetest-unreal",
                        Description = "IceKarma's UnrealIRCD testing network"
                    },
                }
            };
        }

        private void Application_Startup( object sender, StartupEventArgs e ) {
            bool ignoreConf = false;

            if ( e.Args.Length > 0 ) {
                int n = 0;
                while ( n < e.Args.Length ) {
                    if ( e.Args[ n ].Equals( "/ignoreconf", StringComparison.OrdinalIgnoreCase ) ) {
                        ignoreConf = true;
                    }
                    n++;
                }
            }

            if ( !ignoreConf ) {
                try {
                    configuration = Configuration.Storage.Load( "conf.xml" );
                }
                catch ( Exception ex ) {
                    Debug.Print( "App.Application_Startup: exception on config load: {0}", ex );
                }
            }
            if ( null == configuration ) {
                configuration = InventConfiguration( );
                Configuration.Storage.Save( "conf.xml", configuration );
            }

            foreach ( Configuration.Network confNetwork in configuration.Networks ) {
                Protocol.Irc.Network net = new Protocol.Irc.Network( confNetwork );
                Networks.Add( net );
            }
        }

    }

}
